package com.bdqn.t296.noso.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t296.noso.entity.Lpharmacy;


import com.bdqn.t296.noso.entity.Lrecord;
import com.bdqn.t296.noso.service.LdrugService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hospital")
public class LdrugController {
    @Resource
    LdrugService ldrugService;
    @RequestMapping("/drug")
    public String drug() {


        return "hospital/drug";
    }
    @RequestMapping("/selDrug")
    @ResponseBody
   public Object selDrug (Lpharmacy lpharmacy,@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex){
        Page<Lpharmacy> page = new Page<Lpharmacy>(pageIndex-1, 1);
        System.out.println("进来了查询");
        IPage<Lpharmacy> drugPager =ldrugService.selDrug(page,lpharmacy);

        return  drugPager;
    }
    //查询用户的药品信息
    @RequestMapping("/selDrugs")
    @ResponseBody
    public Object selDrugs(Model model,@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,Lrecord lrecord){
        lrecord.setState(2);
        System.out.println("进来了查询药品");
        Page<Lrecord> page = new Page<Lrecord>(pageIndex-1L, 3);
        IPage<Lrecord> selDrugs =ldrugService.selDrugs(page,lrecord);
        return selDrugs ;
    }



    //查询药品库存数量
    @RequestMapping("/selNum")
    @ResponseBody
    public Object selNum(Integer pharmacyId){
        Lpharmacy lpharmacy=new Lpharmacy();
        lpharmacy.setPharmacyId(pharmacyId);

        IPage<Lpharmacy>  listAll =ldrugService.selDrug(null,lpharmacy);
        return listAll;
    }

    //添加药品清单
    @RequestMapping("/addDrug")
    @ResponseBody
    @Transactional
    public Object addDrug(Lrecord lrecord) {
        //查询是否添加过该药品
        lrecord.setState(0);
        int rel = 0;
        //查询是否添加过但未取出该药品
        IPage<Lrecord> lrecords = ldrugService.selDrugs(null,lrecord);
        List<Lrecord>list=lrecords.getRecords();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getDurgname().equals(lrecord.getDurgname())) {
                rel = 1;
                break;
            }
        }
        if (rel == 1) {
            //修改患者药品数量
            int j = ldrugService.updDrug(lrecord);
            if (j == 1) {
                //减少药品数量
                int o = ldrugService.updNum(lrecord);
                if (o == 1) {
                    return "添加成功";
                } else {
                    return "添加失败";
                }
            } else {
                return "添加失败";
            }
        } else {
            //计算总价
            Double qian = lrecord.getRepiceprice();
            Integer shu = lrecord.getDurgnum();
            Double sum = qian * shu;
            lrecord.setRepicetotal(sum);
            lrecord.setState(0);
            //添加药品
            int i = ldrugService.addDrug(lrecord);
            if (i == 1) {
                //减少药品数量
                int j = ldrugService.updNum(lrecord);
                if (j == 1) {
                    return "添加成功";
                } else {
                    return "添加失败";
                }
            } else {
                return "添加失败";
            }
        }
    }

    //移除患者的药品信息
    @RequestMapping("/delDrug")
    @ResponseBody
    public Object delDrug(Integer hospitalpriceid,String durgname,Integer durgnum){
        Lrecord lrecord=new Lrecord();
        lrecord.setHospitalpriceid(hospitalpriceid);
        int i = ldrugService.delDrug(lrecord);
        if (i == 1) {
            lrecord.setDurgname(durgname);
            lrecord.setDurgnum(durgnum);
            int j = ldrugService.updDrugs(lrecord);
            if (j == 1) {
                return "移除成功";
            } else {
                return "移除失败";
            }
        } else {
            return "移除失败";
        }
    }

    //改变患者的药品状态
    @RequestMapping("/updDrug")
    @ResponseBody
    public Object updItem(Lrecord lrecord){
        int upd = ldrugService.upd(lrecord);
        System.out.println(upd);
        return "取药成功";
    }

}
